using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AutoUnzip.Interfaces;

namespace AutoUnzip.Core.Steps
{
    public class DeleteStep : IStep
    {
        private readonly string filesToDelete;

        public DeleteStep(string filesToDelete)
        {
            this.filesToDelete = filesToDelete;
            ProgressMax = 5;
        }

        public bool ShouldExecute { get; set; }
        public long ProgressMax { get; private set; }

        public async Task<bool> ExecuteAsync(string baseDir, CancellationTokenSource cancellationTokenSource, IProgress<long> progress)
        {
            var directoryInfo = new DirectoryInfo(baseDir);
            DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories(filesToDelete, SearchOption.AllDirectories);
            foreach (var dir in directoryInfos)
            {
                dir.Delete(true);
            }

            FileInfo[] fileInfos = directoryInfo.GetFiles(filesToDelete, SearchOption.AllDirectories);
            foreach (var fileInfo in fileInfos)
            {
                await Task.Run(() => fileInfo.Delete());
            }

            progress.Report(ProgressMax);

            return true;
        }


        public override string ToString()
        {
            return string.Format("Delete files: {0}", filesToDelete);
        }
    }
}